﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.Template;
using HF.Framework.SqlDataContract;
using HF.BaseWebCtrlLib;
using HF.WorkFlow.Engine;

public partial class HomeleftPage : System.Web.UI.Page
{
    string UserId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    BaseUserPage.LoadWaitPage();
        //}
        UserId = this.Page.User.Identity.Name;
        if (!this.Page.IsPostBack)
        {
            LoadLeftTree("###", UserId,tvNavigateTree.Nodes);
           
        }
    }
    private string taskTextFormat(string text,string count,string color)
    {
        //<span style="color: red">1</span>
        return string.Format(text+@"<span style='color: "+color+"'>({0})</span>",count); 
    }
    /// <summary>
    /// 递归装载任务导航
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    public  void LoadLeftTree(string key,string userId, TreeNodeCollection startNodes)
    {
        try
        {
            if (key == null || key == "") key = "###";
            string sql = "select * from WebFrkTree where FatherFrkId=@FatherFrkId";   //todo zhuyun
            string FrkId = "";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = sql;
            sqlItem.AppendParameter("@FatherFrkId", key);
            ClientDBAgent agent = new ClientDBAgent();
            DataTable table = agent.ExecuteDataTable(sqlItem);
            WorkFlowInstance flowInstance = new WorkFlowInstance();
            foreach (DataRow row in table.Rows)
            {
                FrkId = row["FrkId"].ToString();
               
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["Caption"].ToString();
                if (FrkId == "2")//未认领任务
                    tmpNode.Text = taskTextFormat(tmpNode.Text, flowInstance.GetClaimTaskCount(userId).ToString(), "red");
                if (FrkId == "3")//已认领任务
                    tmpNode.Text = taskTextFormat(tmpNode.Text, flowInstance.GetClaimedTaskCount(userId).ToString(), "red");
                if (FrkId == "4")//已完成任务
                    tmpNode.Text = taskTextFormat(tmpNode.Text, flowInstance.GetCompletedTaskCount(userId).ToString(), "blue");
                if (FrkId == "6")//我参与的任务
                    tmpNode.Text = taskTextFormat(tmpNode.Text, flowInstance.GetAllTaskCount(userId).ToString(), "blue");
                if (FrkId == "7")//异常终止的任务
                    tmpNode.Text = taskTextFormat(tmpNode.Text, flowInstance.GetAbnormalTaskCount(userId).ToString(), "blue");
                tmpNode.Target = "showframe";
                tmpNode.NavigateUrl = row["NavigateUrl"].ToString();
                tmpNode.ImageUrl = row["ImageUrl"].ToString(); ;
                tmpNode.Value = FrkId;
                startNodes.Add(tmpNode);
                #region 加载启动流程
                if (FrkId == "5")//启动流程
                {
                    string clfathid = "";
                    string cllevel = "";
                    string nowcllevel = "";
                    DataTable startdt = WorkFlowTemplate.GetInstance().GetAllowsStartWorkFlows(userId);
                    foreach (DataRow dr in startdt.Rows)
                    {
                        nowcllevel = dr["classlevel"].ToString();
                        if (nowcllevel == cllevel) continue;
                         clfathid = dr["FatherId"].ToString();
                         if (fatherExist(startdt, clfathid)) continue;
                       
                        LoadWorkflowClass(startdt, nowcllevel, tmpNode.ChildNodes);
                        cllevel = nowcllevel;
                    }
                }
                #endregion

                LoadLeftTree(tmpNode.Value,userId, tmpNode.ChildNodes);
            }
        }
        catch (Exception ex)
        {
            throw new Exception("LoadLeftTree函数," + ex.Message);
        }

    }
    private bool fatherExist(DataTable dt, string key)
    {
       string  filter = "Wfclassid='" + key + "'";
        DataView dv = new DataView(dt);
        dv.RowFilter = filter;
        if (dv!=null&&dv.Count > 0) return true; else return false;
    }
    /// <summary>
    /// 递归装载流程分类
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    public  void LoadWorkflowClass(DataTable dt, string key, TreeNodeCollection startNodes)
    {
        string filter = "";
        string tmpClassId="";
        try
        {
            filter = "classlevel='" + key + "'";
            DataView dv = new DataView(dt);
            dv.RowFilter = filter;
            tmpClassId = "###";
            foreach (DataRowView row in dv)
            {

                string nowClassId = row["WFClassId"].ToString();
                if (tmpClassId == nowClassId) continue;//避免重复添加
                tmpClassId = nowClassId;
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["Caption"].ToString();
                string url = row["mgrurl"].ToString();
                if (url.Length > 0)
                    tmpNode.NavigateUrl = url ;

                tmpNode.Target = "showframe";
                tmpNode.Value = tmpClassId;
                tmpNode.ImageUrl = @"images\tree_27.gif";
                startNodes.Add(tmpNode);
                LoadWorkflow(dt,tmpNode.Value, tmpNode.ChildNodes);
                loadChildClass(dt, tmpClassId, tmpNode.ChildNodes);//递归加载子分类
                
            }
        }
        catch (Exception ex)
        {

            throw new Exception("LoadWorkflowClass函数," + filter + ex.Message);
        }
    }
    public  void loadChildClass(DataTable dt, string key, TreeNodeCollection startNodes)
    {
        string filter = "";
        string tmpClassId = "";
        try
        {
            filter = "FatherId='" + key + "'";
            tmpClassId = "###";
            DataView dv = new DataView(dt);
            dv.RowFilter = filter;
            foreach (DataRowView row in dv)
            {
                string nowClassId = row["WFClassId"].ToString();
                if (tmpClassId == nowClassId) continue;//避免重复添加
                tmpClassId = nowClassId;
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["Caption"].ToString();
                tmpNode.Target = "showframe";
                tmpNode.Value = row["WFClassId"].ToString();
                tmpNode.ImageUrl = @"images\tree_27.gif";
                startNodes.Add(tmpNode);
                LoadWorkflow(dt, tmpNode.Value, tmpNode.ChildNodes);
                loadChildClass(dt, tmpNode.Value, tmpNode.ChildNodes);

            }
        }
        catch (Exception ex)
        {

            throw new Exception("loadChildClass函数," + filter + ex.Message);
        }
    }
    /// <summary>
    /// 加载流程
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    public  void LoadWorkflow(DataTable dt,string key, TreeNodeCollection startNodes)
    {
        string filter = "";
        string tmpflowId = "";
        filter = "WFClassId='" + key + "'";
        tmpflowId = "###";
        DataView dv = new DataView(dt);
        dv.RowFilter = filter;
        try
        {
            foreach (DataRowView row in dv)
            {
                string nowflowid = row["workflowId"].ToString();
                if (tmpflowId == nowflowid) continue;//避免重复添加
                tmpflowId = nowflowid;
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["FlowCaption"].ToString();
                string url=row["mgrurl"].ToString() ;
                if (url.Length>0)
                    tmpNode.NavigateUrl = url ;
                tmpNode.Target = "showframe";
                tmpNode.Value = nowflowid;
                //tmpNode.ImageUrl = @"BasePages\images\workflow.bmp";
                startNodes.Add(tmpNode);
                LoadStartTask(dt, tmpNode.Value, tmpNode.ChildNodes);
            }
        }
        catch (Exception ex)
        {

            throw new Exception("LoadWorkflow函数," + ex.Message);
        }
    }
    /// <summary>
    /// 加载启动节点
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    public  void LoadStartTask(DataTable dt,string key, TreeNodeCollection startNodes)
    {
        string filter = "";
        string tmpTaskId = "";
        filter = "workflowId='" + key + "'";
        tmpTaskId = "###";
        DataView dv = new DataView(dt);
        dv.RowFilter = filter;
        try
        {
            foreach (DataRowView row in dv)
            {
                string nowtaskid = row["WorktaskId"].ToString();
                if (tmpTaskId == nowtaskid) continue;//避免重复添加
                tmpTaskId = nowtaskid;
                string workflowId = row["WorkFlowId"].ToString();
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["TaskCaption"].ToString();
                tmpNode.Target = "_blank";
                
                //tmpNode.ImageUrl = @"BasePages\images\duty.bmp";
                tmpNode.Value = nowtaskid;
                tmpNode.NavigateUrl = @"BasePages\StartWorkflow.aspx?workflowid=" + workflowId + "&worktaskId=" + tmpNode.Value;
                startNodes.Add(tmpNode);

            }
        }
        catch (Exception ex)
        {

            throw new Exception("LoadStartTask函数," + ex.Message);
        }
    }
    private void InitData()
    {
       
    }

    protected void tvNavigateTree_SelectedNodeChanged(object sender, EventArgs e)
    {

    }
}
